package com.whut.gkd.service.impl;

import com.whut.gkd.bean.ApplyShop;
import com.whut.gkd.bean.PageInfo;
import com.whut.gkd.dao.IApplyShopDao;
import com.whut.gkd.service.IApplyShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ApplyShopServicelmpl implements IApplyShopService {

    @Autowired
    private IApplyShopDao applyshopDao;

    @Override
    public PageInfo<ApplyShop> findAll(int currentPage, String applicanName) {
        PageInfo<ApplyShop> pageInfo= new PageInfo();

        int totalCount=applyshopDao.getTotalCount(applicanName);
        pageInfo.setTotalCount(totalCount);
        double d=totalCount/5.0;
        int tp= (int) Math.ceil(d);
        pageInfo.setTotalPage(tp);
        pageInfo.setSize(5);
        if(currentPage<1){
            pageInfo.setCurrentPage(1);
        }else if(currentPage>tp){
            pageInfo.setCurrentPage(tp);
        }else {
            pageInfo.setCurrentPage(currentPage);
        }
        int start=(pageInfo.getCurrentPage()-1)*5;
        if(totalCount!=0){
        List<ApplyShop> shopmanager= applyshopDao.findAll(start,5,applicanName);
        pageInfo.setList(shopmanager);
        }else{
            pageInfo.setList(null);
        }
        return pageInfo;
    }

    @Override
    public void allowshopByapplicanId(int applicanId){

        applyshopDao.allowshopByapplicanId(applicanId);

        ApplyShop applyShop = applyshopDao.getApplyShopById(applicanId);

        applyshopDao.allowshop(applyShop);
    }

    @Override
    public void deleteByapplicanId(int applicanId) {

        applyshopDao.deleteByapplicanId(applicanId);
    }
}
